package eu.liquify.ui {
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import no.doomsday.console.ConsoleUtil;
	public class Box extends UIContainer {
		protected static const HORIZONTAL:int = 0;
		protected static const VERTICAL:int = 1;
		
		protected var boxType:int = HORIZONTAL;
		
		public var drawLines:Boolean = false;
		
		public var fitToGrid:Boolean = true;
		//public var fitToGrid:Boolean = false;
		public var gridWidth:Number = 100;
		public var gridHeight:Number = 22;
		
		public var spacing:Number = 3;
		
		public var labelBox:VBox;
		
		protected var container:DisplayObjectContainer;
		
		protected var labelDisplay:Label;
		
		protected var items:Vector.<DisplayObject> = new Vector.<DisplayObject>();
		
		public function Box() {
			fixedSize = false;
			container = content;
		}
		
		public function set type(v:String):void {
			if (v == "horizontal" || v == "h") boxType = HORIZONTAL;
			else if (v == "vertical" || v == "v") boxType = VERTICAL;
		}
		
		override public function set label(value:String):void {
			super.label = value;
			if (!labelDisplay) addLabel();
		}
		
		private function addLabel():void {
			removeRawChild(container);
			var comps:DisplayObject = Layout.parse(this,
				<comps>
					<vbox id="labelBox">
						<titlelabel label={label} />
					</vbox>
				</comps>
			);
			labelBox.addChild(container);
			container = comps as DisplayObjectContainer;
			addRawChild(container);
		}
		
		override public function addChild(child:DisplayObject):DisplayObject {
			super.addChild(child);
			items.push(child);
			reposition();
			return child;
		}
		
		override public function removeChild(child:DisplayObject):DisplayObject {
			super.removeChild(child);
			items.splice(items.indexOf(child), 1);
			reposition();
			return child;
		}
		
		override public function resize():void {
			super.resize();
			for (var i:int = 0; i < items.length; i++) {
				var d:DisplayObject = items[i];
				if (d is UIItem) {
					var item:UIItem = d as UIItem;
					item.resize();
				}
			}
			reposition();
		}
		
		protected function reposition():void {
			var i:int;
			var w:Number = 0;
			var h:Number = 0;
			var max:Number = 0;
			
			var d:DisplayObject;
			
			var g:Graphics = content.graphics;
			g.clear();
			//g.lineStyle(1, 0xFF0000);
			//g.lineStyle(1, 0xFF0000);
			var coords:Vector.<Number> = new Vector.<Number>();
			
			for (i = 0; i < items.length; i++) {
				d = items[i];
				
				d.x = w;
				d.y = h;
				
				var item:UIItem = d as UIItem;
				if (item) {
					//item.fixedSize = fixedSize;
					if (autosizeCascade) {
						item.fixedSize = false;
						if (item is UIContainer) {
							(item as UIContainer).autosizeCascade = true;
						}
					}
					if (fitToGrid) {
						item.width = gridWidth;
						item.height = gridHeight;
					}
					item.resize();
				}
					
				if (boxType == HORIZONTAL) {
					w += (fitToGrid ? gridWidth : d.width)+spacing;
					if (d.height > max) max = d.height;
					if (drawLines) coords.push(w);
				} else {
					h += (fitToGrid ? gridHeight : d.height)+spacing;
					if (d.width > max) max = d.width;
					if (drawLines) coords.push(h);
				}
			}
			
			if (drawLines) {
				var m:Matrix = new Matrix();
				if (boxType == HORIZONTAL) {
					m.createGradientBox(1, max, Math.PI/2);
				} else {
					m.createGradientBox(max, 1);
				}
				//g.lineStyle(1);
				//g.lineGradientStyle(GradientType.LINEAR, [0x000000, 0x000000, 0x000000, 0x000000], [0, 0.2, 0.2, 0], [0, 50, 205, 255], m);
				//g.lineGradientStyle(GradientType.LINEAR, [0x000000, 0x000000, 0x000000], [0, 0.15, 0], [0, 127, 255], m);
				g.lineStyle(1, 0x000000, 0.1);
				for (i = 0; i < coords.length-1; i++) {
					var coord:Number = coords[i]-spacing/2;
					if (boxType == HORIZONTAL) {
						g.moveTo(coord, 0);
						g.lineTo(coord, max);
					} else {
						g.moveTo(0, coord);
						g.lineTo(max, coord);
					}
				}
			}
			
			if (!fixedSize && autosize) {
				if (boxType == HORIZONTAL) {
					//width = fitToGrid ? w : w-spacing;
					width = w-spacing;
					height = max;
					//height = fitToGrid ? gridHeight : max;
					//height = fitToGrid ? Math.max(max, gridHeight) : max;
				} else {
					//height = fitToGrid ? h : h-spacing;
					height = h-spacing;
					width = max;
					//width = fitToGrid ? gridWidth : max;
					//width = fitToGrid ? Math.max(max, gridWidth) : max;
				}
			}
			
			//redraw();
		}
		
		private function redraw():void {
			var g:Graphics = graphics;
			g.clear();
			g.beginFill(0x000000, 0.1);
			g.drawRect(0, 0, width, height);
		}
		
	}

}